#include "ItemEntity.h"
#include "EffectManager.h"



void ItemEntity::_checkActive()
{
	double x = _data->rect->X;
	double targetX = _data->getTargetRect()->X;

	if (abs(x - targetX) < _data->rangeOfEffect) {
		_data->isActived = true;
	}
}

void ItemEntity::_checkState()
{
	double x = _data->rect->X;
	double targetX = _data->getTargetRect()->X;
}

ItemEntity::ItemEntity(int id, int itemType, double x, double y, Rect* targetRect)
{
	_data = new ItemData(id, itemType, x, y, targetRect);
	_physics = new ItemPhysics(_data);	
	_effectivePhysics = new ItemEffectivePhysics(_data);
	isStatic = false;
	_currentPhysics = _physics;
	// init demo value for test only
}

void ItemEntity::update(long time)
{
	if (isStatic) return;
	if (_data->isActived) {
		_currentPhysics->update();
		_checkState();
	} else {
		_checkActive();
	}
}

void ItemEntity::draw(long time)
{
	if (_data->isDead) return;
	if (_data->isActived) {
		_data->sprites->draw();
	}
}

AbstractData* ItemEntity::getAbstractData()
{
	return _data;
}

ItemData* ItemEntity::getItemData()
{
	return _data;
}

void ItemEntity::updateData(int action, int action_data)
{
	if (action == ACTIONS_STOP_FLYING) {
		isStatic = true; 
	}
	if (action == ACTIONS_IS_HURT) {
		EffectManager::getInstatnse()->createEffect(EffectType::TYPE_Object_Burning, _data->getRect()->X, _data->getRect()->Y);
		_data->sprites->currentSprite->setIndex(_data->itemRandom - _ID_ITEM_BLOCK);
		_currentPhysics = _effectivePhysics;
	}
	if (action == ACTIONS_HIT_PLAYER) {
		_data->isActived = false;
		_data->isDead = true;
	}
}

void ItemEntity::initData()
{

}
